#include "PrecompiledHeader.h"
#include "IRender.h"

/*--------------------------------------------------------------------------*/
glm::mat4 IRender::identityMatrix = glm::mat4();
/*--------------------------------------------------------------------------*/

IRender::IRender(uint flags) :
	m_flags(flags)
{

}

/*--------------------------------------------------------------------------*/

IRender::~IRender()
{
	this->ClearCustomTextures();
	this->ClearTasks();
}

/*--------------------------------------------------------------------------*/

void IRender::AddRenderTask(const RenderTaskDesc& renderTaskDesc)
{
	AssertLogW(renderTaskDesc.m_priority != 0
	           && (renderTaskDesc.m_flags & ERenderTaskFlag::SortByDist || renderTaskDesc.m_flags & ERenderTaskFlag::InvSortByDist),
	           "Priority value will be ignored (used flag SortByDist or InvSortByDist)");

	this->m_tasks.push_back(RenderTask(renderTaskDesc));
}

/*--------------------------------------------------------------------------*/

void IRender::ClearTasks()
{
	this->m_tasks.clear();
}

/*--------------------------------------------------------------------------*/

void IRender::BindRenderTasks(IRenderPhase* rp)
{
	for (std::vector<RenderTask>::iterator iter = this->m_tasks.begin(); iter != this->m_tasks.end(); ++iter)
	{
		rp->AddTask(&(*iter));
	}
}

/*--------------------------------------------------------------------------*/

void IRender::BindRenderTask(IRenderPhase* rp, uint i)
{
	for (std::vector<RenderTask>::iterator iter = this->m_tasks.begin(); iter != this->m_tasks.end(); ++iter)
	{
		if (iter->GetArgument() == i)
		{
			rp->AddTask(&(*iter));
			return;
		}
	}
}

/*--------------------------------------------------------------------------*/

void IRender::AddCustomTexture(const std::string& name, GLTexture* t)
{
	this->m_customTextures[name] = t;
}

/*--------------------------------------------------------------------------*/

void IRender::ClearCustomTextures()
{
	this->m_customTextures.clear();
}

/*--------------------------------------------------------------------------*/

void IRender::BindCustomTextures()
{
	for (auto iter = this->m_customTextures.begin(); iter != this->m_customTextures.end(); ++iter)
	{
		ROpenGL::SetSampler(iter->first, iter->second);
	}
}

/*--------------------------------------------------------------------------*/

void IRender::OnPreRender(const RenderTask* /*renderTask*/)
{

}

/*--------------------------------------------------------------------------*/

void IRender::OnPostRender(const RenderTask* /*renderTask*/)
{

}

/*--------------------------------------------------------------------------*/

const glm::mat4& IRender::GetModelMatrix() const
{
	return identityMatrix;
}

/*--------------------------------------------------------------------------*/

void IRender::CalcModelMatrices()
{

}

/*--------------------------------------------------------------------------*/